package com.orangecy.common.context;

import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import com.orangecy.common.exception.OrangeCyException;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * @Author : orangeCy.奕
 * @Time : 2021/9/18
 * @desc : 猎豹养成产品源码
 */
@Data
public class FeatureDataMap {

    private Map<String,Object> cacheMap;

    /**
     * 读取元素
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public final <T> T get(RunDataKey key, Class<T> clazz) {
        if(null == key) {
            throw new OrangeCyException("get的key不能为空");
        }
        if(null == clazz) {
            throw new OrangeCyException("get的clazz不能为空");
        }
        if (key.isArray()) {
            throw new OrangeCyException("集合类型请用getList方法");
        }
        validateClazz(key, clazz);
        if(null == cacheMap) {
            return null;
        }
        return (T) cacheMap.get(key.key());
    }

    /**
     * 存放元素
     * @param key
     * @param value
     * @param <T>
     */
    public final <T> void put(RunDataKey key, T value) {
        if(null == key) {
            throw new OrangeCyException("put的key不能为空");
        }
        if (null == value) {
            throw new OrangeCyException("put的value不能为空");
        }
        if (key.isArray()) {
            throw new OrangeCyException("集合类型请用putList方法");
        }
        validateClazz(key, value.getClass());
        if(null == cacheMap) {
            cacheMap = Maps.newHashMap();
        }
        cacheMap.put(key.key(), value);
    }

    /**
     * 读取集合元素
     * @param key
     * @param clazz
     * @param <T>
     * @return
     */
    public final <T> List<T> getList(RunDataKey key, Class<T> clazz) {
        if (null == key) {
            throw new OrangeCyException("getList的key不能为空");
        }
        if (null == clazz) {
            throw new OrangeCyException("getList的clazz不能为空");
        }
        if (!key.isArray()) {
            throw new OrangeCyException("非集合类型请用get方法");
        }
        validateClazz(key, clazz);
        if(null == cacheMap) {
            return null;
        }
        return (List<T>) cacheMap.get(key.key());
    }

    /**
     * 存放集合元素
     * @param key
     * @param value
     * @param clazz
     * @param <T>
     */
    public final <T> void putList(RunDataKey key, List<T> value, Class<T> clazz) {
        if(null == key) {
            throw new OrangeCyException("putList的key不能为空");
        }
        if (null == value) {
            throw new OrangeCyException("putList的value不能为空");
        }
        if (null == clazz) {
            throw new OrangeCyException("putList的泛型必须指定");
        }
        if (!key.isArray()) {
            throw new OrangeCyException("非集合类型请用put方法");
        }
        validateClazz(key, clazz);
        if(null == cacheMap) {
            cacheMap = Maps.newHashMap();
        }
        cacheMap.put(key.key(), value);
    }

    private <T> void validateClazz(RunDataKey key, Class<T> clazz) {
        if (!Objects.equal(key.clazz(), clazz)) {
            throw new OrangeCyException("类型不符合");
        }
    }
}